<?php

namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

/**
 * GroupChatController
 *
 * @author
 *
 * @version
 *
 */
class GroupchatController extends AbstractActionController {
	/**
	 * The default action - show the home page
	 */
	protected $authservice;
	
	public function indexAction() {
		// TODO Auto-generated GroupChatController::indexAction() default action
		//$user = $this->getLoggedInUser(); ???????????????????
		//getting user and authentication**************************************
		$userTable = $this->getServiceLocator()->get('Users\Model\UserTable');
		// Get User Info from Session
		$userEmail = $this->getAuthService()->getStorage()->read();
		$user = $userTable->getUserByEmail($userEmail);
		//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('b��d'.var_dump($user->name));
		//*********************************************************************
		$request = $this->getRequest();
		if ($request->isPost()) {
			$messageTest = $request->getPost()->get('message');
			$fromUserId = $user->id;
			$this->sendMessage($messageTest, $fromUserId);
			// to prevent duplicate entries on refresh
			return $this->redirect()->toRoute('groupchat');
		}
		//Prepare Send Message Form
		$form = new \Zend\Form\Form();
		$form->add(array(
				'name' => 'message',
				'attributes' => array(
						'type' => 'text',
						'id' => 'messageText',
						'required' => 'required'
				),
				'options' => array(
						'label' => 'Message',
				),
		));
		$form->add(array(
				'name' => 'submit',
				'attributes' => array(
						'type' => 'submit',
						'value' => 'Send'
				),
		));
		$form->add(array(
				'name' => 'refresh',
				'attributes' => array(
						'type' => 'button',
						'id' => 'btnRefresh',
						'value' => 'Refresh'
				),
		));
			$viewModel = new ViewModel(array('form' => $form,'userName' => $user->name));
				
		return $viewModel;
	}
	
	public function getAuthService()
	{
		if (! $this->authservice) {
			$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
			$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter,'user','email','password', 'MD5(?)');
			$authService = new AuthenticationService();
			$authService->setAdapter($dbTableAuthAdapter);
			$this->authservice = $authService;
		}
		return $this->authservice;
	}
	
	public function messageListAction()
	{
		$userTable = $this->getServiceLocator()->get('Users\Model\UserTable');
		$chatMessageTG = $this->getServiceLocator()->get('ChatMessagesTableGateway');
		$chatMessages = $chatMessageTG->select();
		$messageList = array();
		
		//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('b��d'.var_dump($chat->message));
		foreach($chatMessages as $chatMessage) {
			$fromUser = $userTable->getUser($chatMessage->user_id);	
			$messageData = array();
			$messageData['user'] = $fromUser->name;
			$messageData['time'] = $chatMessage->stamp;
			$messageData['data'] = $chatMessage->message;
			$messageList[] = $messageData;
			//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('b��d'.var_dump($messageList));
		}
		//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('b��d'.var_dump($messageList[1]->message));
		$viewModel = new ViewModel(array('messageList' =>$messageList));
		//$viewModel->setTemplate('groupchat/message-list');
		$viewModel->setTerminal(true);
		return $viewModel;
	}
		
	protected function sendMessage($messageTest, $fromUserId)
		{
			$chatMessageTG = $this->getServiceLocator()->get('ChatMessagesTableGateway');
			$data = array(
					'user_id' => $fromUserId,
					'message' => $messageTest,
					'stamp' => NULL
			);
			$chatMessageTG->insert($data);
			return true;
	}
}